package com.qimo.zzy.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qimo.zzy.dao.ProductDao;
import com.qimo.zzy.entity.ResultEntity;
import com.qimo.zzy.tool.JieBaTool;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;

import java.util.*;


@Service
public class ProductService {
    private static final Logger logger = LoggerFactory.getLogger(UserService.class);//日志。getLogger需要传入当前的类名

    @Resource
    ProductDao productDao;
    @Resource
    JieBaTool jieBaTool;

    //获取商品列表
    public String getProductList(String detail, int start, int cnt, int sort) {
        ArrayList<String> productEntities = new ArrayList<>();
        try {
            if (!Objects.equals(detail, "")) {
                List<String> ac = jieBaTool.BaseJiaBa(detail);
                for (String i : ac) {
                    productEntities.add(ResultEntity.success(productDao.getProductList(i, start, cnt, sort)));
                    List<String> a = productDao.getSort(i);
                    for (String j : a) {
                        int b = Integer.parseInt(j);
                        productEntities.add(ResultEntity.success(productDao.getProductList("", start, cnt, b)));
                    }
                }
            } else {
                productEntities.add(ResultEntity.success(productDao.getProductList(detail, start, cnt, sort)));
            }
            //采用迭代器的模式
            Iterator<String> it = productEntities.iterator();
            Set<Integer> set = new HashSet<>();
            while (it.hasNext()) {
                String a = it.next();
                JSONObject jo = JSON.parseObject(a);
                JSONArray jsar = jo.getJSONArray("data");
                System.out.println(jsar);
                if (!jsar.isEmpty()) {
                    Iterator<Object> its = jsar.iterator();
                    int i = 0;
                    while (its.hasNext()) {
                        JSONObject ao = (JSONObject) its.next();
                        String str = ao.getString("pid");
                        int pid = Integer.parseInt(str);
                        if (!set.add(pid)) {
                            it.remove();
                        }
                    }
                } else {
                    it.remove();
                }
            }
            return productEntities.toString();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }

    //获取商品详情
    public String getProductDetail(int pid) {
        try {
            return ResultEntity.success(productDao.getProductDetail(pid));
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
    public String getProductImage(int pid){
        try {
            return ResultEntity.success(productDao.getProductImage(pid));
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getLocalizedMessage());
            return ResultEntity.fail(-10004);
        }
    }
}
